package com.gsm.controller;

import com.gsm.entity.Result;
import com.gsm.entity.SysUser;
import com.gsm.po.Course;
import com.gsm.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程(Course)表控制层
 *
 * @author wjq
 * @since 2020-11-30 03:29:32
 */
@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 新增
     */
    @PostMapping("course")
    public Result insertCourse(@RequestBody Course course,@RequestAttribute("user") SysUser u) {
        if(course.getCreateUser().longValue()!=u.getUserId().longValue()){
            System.out.println("登录用户有误");
            course.setCreateUser(u.getUserId());
        }
        Result result = courseService.insertCourse(course);
        return result;
    }

    /**
     * 根据 id 删除数据
     */
    @DeleteMapping("course/{courseId}")
    public Result deleteCourseByCourseId(@PathVariable("courseId") Long courseId) {
        Result result = courseService.deleteCourseByCourseId(courseId);
        return result;
    }

    /**
     * 根据 id 修改单条数据
     */
    @PutMapping("course/{courseId}")
    public Result updateCourseByCourseId(@PathVariable("courseId") Long courseId, @RequestBody Course course) {
        course.setCourseId(courseId);
        Result result = courseService.updateCourseByCourseId(course);
        return result;
    }

    /**
     * 根据 id 查询单条数据
     */
    @GetMapping("course/{courseId}")
    public Result selectCourseByCourseId(@PathVariable("courseId") Long courseId) {
        Result result = courseService.selectCourseByCourseId(courseId);
        return result;
    }


    /**
     * 根据条件查询 List
     */
    @GetMapping("courseList")
    public Result selectCourseList(Course course) {
        Result result = courseService.selectCourseList(course);
        return result;
    }

    /**
     * 根据章节id 条件查询 List
     */
    @GetMapping("courseList/{chapterId}")
    public Result selectCourseListByChapterId(@PathVariable("chapterId") Long chapterId) {
        Result result = courseService.selectCourseListByChapterId(chapterId);
        return result;
    }

    /**
     * 根据id排序
     */
    @PutMapping("order")
    public Result updateCourseShowOrder(Long subjectId,Long chapterId,@RequestBody List<Long> ids) {
        Result result = courseService.updateCourseShowOrder(subjectId,chapterId,ids);
        return result;
    }
}